<?php

namespace Modules\Car\Http\Controllers;

use App\Helpers\ResponseData;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use AlibabaCloud\SDK\Ocr\V20191230\Ocr;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Ocr\V20191230\Models\RecognizeDrivingLicenseRequest;
use AlibabaCloud\SDK\Ocr\V20191230\Models\RecognizeIdentityCardRequest;
use AlibabaCloud\SDK\Ocr\V20191230\Models\RecognizeDriverLicenseRequest;
use Modules\Car\Entities\RecognizeDrivingLicense;
use mysql_xdevapi\Exception;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
//composer require alibabacloud/ocr-20191230 扩展包
//行驶证识别接口
class RecognizeDrivingController extends Controller
{
    use ResponseData;
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Ocr Client
     */
    protected function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "ocr.cn-shanghai.aliyuncs.com";
        return new Ocr($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
//http(s)://[Endpoint]/?Action=RecognizeDrivingLicense
//&ImageURL=https://viapi-test.oss-cn-shanghai.aliyuncs.com/test-team/tiankong/4%E8%A1%8C%E9%A9%B6%E8%AF%81.jpg
//&Side=face
//&<公共请求参数>
    public  function driving_info(Request $request){
        // 复制代码运行请自行打印 API 的返回值
        try{
            $data = cache()->remember($request->image, 30, function ()use($request){
                $oss = config('aliyun.web');
                $client = $this->createClient($oss['accessKeyId'], $oss['accessKeySecret']);
                $recognizeDrivingLicenseRequest = new RecognizeDrivingLicenseRequest([
                    "imageURL" => $request->image,
                    "side" => $request->type
                ]);
                $res = $client->recognizeDrivingLicense($recognizeDrivingLicenseRequest);
                return $res->body;
            });
            $is_set = RecognizeDrivingLicense::query()->where('plate_number',$data->data->faceResult->plateNumber)->first();
            if($is_set){
                $data->data->faceResult->is_set = 1;
            }else{
                $data->data->faceResult->is_set = 0;
            }
            return $this->success($data);
        }catch (\Exception $exception){
            if($exception->getCode()==417){
                return $this->failed('请求频繁，请稍后再试',201);
            }else{
                return $this->failed($exception->getMessage(),201);
            }
        }

    }
    //获取身份证信息接口
    public function recognizeIdentityCard(Request $request){
        // 复制代码运行请自行打印 API 的返回值
        try{
            $data = cache()->remember('card:'.$request->image, 30, function ()use($request){
                $oss = config('aliyun.web');
                $client = $this->createClient($oss['accessKeyId'], $oss['accessKeySecret']);
                $recognizeDrivingLicenseRequest = new RecognizeIdentityCardRequest([
                    "imageURL" => $request->image,
                    "side" => $request->type
                ]);
                $res = $client->recognizeIdentityCard($recognizeDrivingLicenseRequest);
                return $res->body;
            });
            return $this->success($data);
        }catch (\Exception $exception){
            if($exception->getCode()==417){
                return $this->failed('请求频繁，请稍后再试',201);
            }else{
                return $this->failed($exception->getMessage(),201);
            }
        }
    }
    public function driverLicense(Request $request){
        try{
            $data = cache()->remember('card:'.$request->image, 30, function ()use($request){
                $oss = config('aliyun.web');
                $client = $this->createClient($oss['accessKeyId'], $oss['accessKeySecret']);
                $recognizeDrivingLicenseRequest = new RecognizeDriverLicenseRequest([
                    "imageURL" => $request->image,
                    "side" => $request->type
                ]);
                $res = $client->recognizeDriverLicense($recognizeDrivingLicenseRequest);
                return $res->body;
            });
            return $this->success($data);
        }catch (\Exception $exception){
            if($exception->getCode()==417){
                return $this->failed('请求频繁，请稍后再试',201);
            }else{
                return $this->failed($exception->getMessage(),201);
            }
        }
    }
}
